package com.pharmacy.controller;


import com.pharmacy.constant.JwtClaimsConstant;
import com.pharmacy.dto.*;
import com.pharmacy.entity.Employees;
import com.pharmacy.properties.JwtAttribute;
import com.pharmacy.result.PageResult;
import com.pharmacy.result.Result;
import com.pharmacy.service.EmployeesService;
import com.pharmacy.utils.JwtUtil;
import com.pharmacy.vo.EmployeesLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("/admin/employees")
@Slf4j
@Tag(name = "员工管理")
public class EmployeesController {

    @Autowired
    private EmployeesService employeesService;

    @Autowired
    private JwtAttribute jwtAttribute;

    @Operation(summary = "员工登录")
    @PostMapping("/login")
    public Result<EmployeesLoginVo> login(@RequestBody EmployeesLoginDto employeesLoginDto){
        log.info("员工登录{}",employeesLoginDto);
        Employees employees = employeesService.login(employeesLoginDto);

        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employees.getId());

        String token = JwtUtil.createJWT(jwtAttribute.getAdminSecretKey(), jwtAttribute.getAdminTtl(), claims);

        EmployeesLoginVo employeesLoginVo = EmployeesLoginVo.builder()
                .id(employees.getId())
                .username(employees.getUsername())
                .name(employees.getName())
                .token(token)
                .build();

        return Result.success(employeesLoginVo);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @Operation(summary = "员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeesAddDto
     * @return
     */
    @PostMapping("/useradd")
    @Operation(summary = "新增员工")
    public Result addEmployees(@RequestBody EmployeesAddDto employeesAddDto){
        log.info("添加员工{}",employeesAddDto);
        employeesService.addEmployees(employeesAddDto);
        return Result.success();
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @Operation(summary = "删除员工")
    @DeleteMapping("/{id}")
    public Result deleteEmployees(@PathVariable Long id){
        log.info("删除员工{}",id);
        employeesService.deleteEmployees(id);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param employeesUpdateDto
     * @return
     */

    @Operation(summary = "修改员工信息")
    @PutMapping("/updateEmployees")
    public Result updateEmployees(@RequestBody EmployeesUpdateDto employeesUpdateDto){
        log.info("修改员工信息{}",employeesUpdateDto);
        employeesService.updateEmployees(employeesUpdateDto);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param employeesPageSelectDto
     * @return
     */

    @Operation(summary = "员工分页查询")
    @GetMapping("/pageSelect")
    public Result<PageResult> pageSelect(EmployeesPageSelectDto employeesPageSelectDto){
        log.info("员工分页查询{}",employeesPageSelectDto);
        PageResult pageResult = employeesService.pageSelect(employeesPageSelectDto);
        return Result.success(pageResult);
    }

    /**
     * 修改密码
     * @param updatePasswordDto
     * @return
     */
    @Operation(summary = "修改密码")
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto){
        log.info("修改密码",updatePasswordDto);
        employeesService.updatePassword(updatePasswordDto);
        return Result.success();
    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询信息")
    @GetMapping("/{id}")
    public Result<Employees> getById(@PathVariable Integer id){
        Employees employees = employeesService.getById(id);
        return Result.success(employees);
    }

}
